function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function updateCookie (divIndex) {
    createCookie("opened_categories", divIndex);
} 

/*function getDivState (elem) {
if ($(elem) != null) return ($(elem).style.display == 'block') ? 1 : 0; //else throw (elem + " not found");

}
*/
//read cookie
var category_conf = readCookie("opened_categories"); 
var category_conf_vars = new Array(0);
if (category_conf) { category_conf_vars = category_conf.split(","); }


function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}
function initialize(){
	//document.getElementById("warcraftgamebuttons").style.display = "none";
	//document.getElementById("diablogamebuttons").style.display = "none";
	//document.getElementById("arcadegamebuttons").style.display = "none";
}

function showHide(whichElement, whichArrow){
	if(document.getElementById(whichElement).style.display!="block"){
		document.getElementById(whichElement).style.display="block";
		document.getElementById(whichArrow).className="gameuniverse_arrow_up";
	} else {
		document.getElementById(whichElement).style.display="none";
		document.getElementById(whichArrow).className="gameuniverse_arrow_down";
	}
}

function showHideContact(){
	if(document.getElementById("contactcontent").style.display!="block"){
		$("contactcontent").style.display="block";
		$("contactarrow").style.backgroundImage = "url('images/contactbar_arrow.gif')";
		$("contactarrow").style.backgroundPosition = "right -9px";
	} else {
		$("contactcontent").style.display="none";
		$("contactarrow").style.backgroundImage = "url('images/contactbar_arrow.gif')";
		$("contactarrow").style.backgroundPosition = "right top";
	}
}

function toggleContainer (elemNumber, elem) { /*controls main categories boxes behaviour*/

    var parentDiv = elemNumber + "-parentDiv";
    var elemLink = elemNumber + "-link";
    var div = elemNumber + "-div";

    $(parentDiv).onmouseover = '';
    $(parentDiv).onmouseout = '';
    
    if ($(div).style.display != 'block') {
        $(div).style.display = 'block';
		elem.className="minus";
    } else {
        $(div).style.display = 'none';
		elem.className="plus";
    }
    var elementList = $('categories').getElementsByTagName('DIV');
    for (var i=0; i < elementList.length; i++) {
        var element = elementList[i];
        var elemId = element.id.substr(0, element.id.length-10);        
        if ((element.getAttribute("class") == "categoryContainer" || element.getAttribute("className") == "categoryContainer") && element.id != parentDiv) {
            resetContainerStyle(element);            
        } else if ((element.getAttribute("class") == "categoryContainer active" || element.getAttribute("className") == "categoryContainer active") && element.id != parentDiv) { 
            element.style.borderWidth = "1px";        
            element.style.borderStyle = "solid";        
            element.style.borderColor = "#e3e3e3";        
            element.style.backgroundColor = "#f6f6f6";
            $(elemId + '-link').className = "plus";
            $(elemId + '-div').style.display = "none";
        }
/*    updateCookie(elemNumber);*/
    }
}
function activateLink (linkElem, parentDiv) { /*makes current category bold resets other boxes*/
    var containerList = $('categories').getElementsByTagName("DIV");
    for (var i = 0; (container = containerList[i]); i++) {
        if ((container.getAttribute("class") == "categoryContainer active" || container.getAttribute("className") == "categoryContainer active") && container.id != parentDiv) { 
            resetContainerStyle(container);
        } 
    }
    $(parentDiv).className = "categoryContainer active";    
    var linkList = $('categories').getElementsByTagName("A");
    for (var i = 0; (theLink = linkList[i]); i++) {
        if (theLink.getAttribute("class") == "categoryLink" || theLink.getAttribute("className") == "categoryLink") theLink.style.fontWeight = 'normal';
    }
    linkElem.style.fontWeight = 'bold';
    if ($('issuelisting').style.display == 'none') $('issuelisting').style.display = "block";
}

function resetContainerStyle (element) {

    var elemId = element.id.substr(0, element.id.length-10);
/*    alert(elemId);*/
    element.className = "categoryContainer";
    element.style.borderWidth = "1px";        
    element.style.borderStyle = "solid";        
    element.style.borderColor = "#ffffff";        
    element.style.backgroundColor = "#ffffff";
/*    alert("childnode0:" + element.childNodes[0].id + "\n\rchildnode1:" + element.childNodes[1].id);*/
    $(elemId + '-link').className = "plus";
    $(elemId + '-div').style.display = "none";
    element.onmouseover = function (event) {this.style.backgroundColor ='#f6f6f6'; this.style.borderStyle = 'solid'; this.style.borderWidth = '1px'; this.style.borderColor = '#e3e3e3';}
    element.onmouseout = function (event) {this.style.backgroundColor = '#ffffff'; this.style.borderWidth = '1px'; this.style.borderStyle = 'solid'; this.style.borderColor = '#ffffff';}
}



var suggestedTimeout;
var itsOn = false;

function initEventTimeout(element){
	itsOn = true;
	window.setTimeout('fadeInElement("' + element + '")', 100);
	if(suggestedTimeout != null) window.clearTimeout(suggestedTimeout);
}
function clearEventTimeout(element) {
	itsOn = false;
	suggestedTimeout = window.setTimeout('fadeOutElement("' + element + '")', 300);
}

function fadeInElement(elem){ $(elem).style.display="block"; }
function fadeOutElement(elem){ if(!itsOn){ $(elem).style.display="none"; } }

function changeFlag(whichFlag){
	document.getElementById("langselectflag").style.backgroundImage='url(images/'+whichFlag+')';
}

function $ (elem) {
	return document.getElementById(elem);
}

function addValues1 () {
	var aList = $('productlisttop').getElementsByTagName('A');
	var counter = 0;
		for (var i=0; i < aList.length; i++){
			aList[i].onclick = function () {
                document.forms['searchForm1'].gameId.value = this.getAttribute("gameId");
				$('allProducts_productlisttop').innerHTML = "";
				$('allProducts_productlisttop').innerHTML = this.firstChild.nodeValue;
				$('productlisttop').style.display = "none";
				return false;
			}
		counter++;
		}
}
function addValues2 () {
	var aList = $('productlistbottom').getElementsByTagName('A');
	var counter = 0;
		for (var i=0; i < aList.length; i++){
			aList[i].onclick = function () {
                document.forms['searchForm2'].gameId.value = this.getAttribute("gameId");
				$('allProducts_productlistbottom').innerHTML = "";
				$('allProducts_productlistbottom').innerHTML = this.firstChild.nodeValue;
				$('productlistbottom').style.display = "none";
				return false;
			}
		counter++;
		}
}

var myWin;
function openWin (url) {
  myWin = window.open(url, "print", "width=800,height=400,scrollbars=yes");
  myWin.focus();
}


var xmlDataArray = new Array;
var	myXMLHTTPRequest;
var	myXMLHTTPRequest2; 
var intervalHolder;
var containerGlobal;
var loaderGlobal;


function dummyFunction(){}

function toggleMenu(elem) {
    classOn = elem.id+ "-ovr";
    classOff = elem.id+ "-off";
    elemDrop = elem.id+ "-drop";
    var obj;
    var obj2;
    if (document.getElementById(elemDrop)) {
        obj = document.getElementById(elemDrop);
        if (obj.style.visibility != "visible" || obj.style.display == "none") {
            obj.style.visibility = "visible";
            obj.style.display = "block";
            obj.parentNode.className = classOn;
        } 
        else { 
            obj.style.visibility = "hidden";
            obj.style.display = "none";
            obj.parentNode.className = classOff;
//            obj.style.height = "0px";
        }
    } else {
        obj2 = document.getElementById(elem.id);
        if (obj2.className != classOn) {
            obj2.className = classOn;
        } else obj2.className = classOff;
    }
}

// Get maxlength of longest linkname in the unordered list and resize the list -->
function resizeList() { 
    var maxLength = 0;
    var lists = document.getElementsByTagName("ul"); 
    for (x = 0; x < lists.length; x++) { 
        var list = lists[x]; 
		if (list.getAttribute("class") == "linkList" || list.getAttribute("className") == "linkList") { 
            maxLength = 0;
            parentDiv = list.parentNode.parentNode.parentNode;
            var listitems = list.childNodes;
			for (j = 0; j < listitems.length; j++) {
				node = listitems[j];
                if (node.nodeName == "LI") {
					linkitems = node.childNodes;
                    for (k = 0; k < linkitems.length; k++) {
						linkitem = linkitems[k];
		                if (linkitem.nodeName == "SPAN") {
                            subHeader = linkitem.childNodes[0].nodeValue;
							if ((subHeader.length*1.5) > maxLength) { 
                                maxLength = subHeader.length * 1.5;
                            }
                        }
                        if (linkitem.nodeName == "UL") {
                            reallist = linkitem.childNodes;
                            for (f = 0; f < reallist.length; f++) {
                                reallistitems = reallist[f];
                                if (reallistitems.nodeName == "LI") { 
                                    reallistlinks = reallistitems.childNodes;
                                    for (g = 0; g < reallistlinks.length; g++) {
                                        reallink = reallistlinks[g];
                                        if (reallink.nodeName == "A") { 
                                            linkValue = reallink.childNodes[0].nodeValue;
                                            if (linkValue.length > maxLength) {
                                                maxLength = linkValue.length;
                                            }
                                        }
                                    }
                                }
                            }
                        }
					}
	            }
            }
        if (maxLength > parentDiv.style.width) parentDiv.style.width = ((maxLength/5)*2+2) + "em";
		}
    } 
}

function stretchContainer () {
    var normalWidthArticleContainer = 688;    

    var maxWidth = 0;
    var imgList = $('articlecontent').getElementsByTagName('IMG');
    for (var i = 0; i < imgList.length; i++) {
        if(imgList[i].width > maxWidth) maxWidth = imgList[i].width;
    }
    if (maxWidth > normalWidthArticleContainer) $('articlecontent').style.width = (maxWidth) + "px";
}

var gamesArray = new Array("",
       "Diablo II &amp; Expansion",
       "Diablo",
       "StarCraft &amp; Expansion",
       "Warcraft II Battle.net Edition",
       "Warcraft III &amp; Expansion",
       "Blackthorne",
       "Rock 'n Roll Racing",
       "The Lost Vikings",
       "Warcraft",
       "Warcraft II &amp; Expansion","World of Warcraft &amp; Expansion");

function updateSearchbar(){
	if(jsGameWorldId != -1){
        getSearchLabel();
    	var jsGameWorld = gamesArray[jsGameWorldId];
		document.forms['searchForm1'].gameId.value = jsGameWorldId;
		$('allProducts_productlisttop').innerHTML = jsGameWorld;
		if(document.forms['searchForm2']) document.forms['searchForm2'].gameId.value = jsGameWorldId;
		if(document.forms['searchForm2']) $('allProducts_productlistbottom').innerHTML = jsGameWorld;
	}

}

/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/

var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

} 

addEvent(window, 'load', resizeList);
addEvent(window, 'load', updateSearchbar);



